package com.example.leetcode.c1_c100.c75;

import java.util.Arrays;

/**
 * 颜色分类
 * @author zhanpengguo
 * @date 2020-09-18 16:03
 */
public class Solution {

    public void sortColors(int[] nums) {
        // 使用三个指针，当前，0的右边界，2的左边界
        int cur = 0;
        int zeroRight = 0;
        int twoLeft = nums.length - 1;
        while (cur <= twoLeft) {
            if (nums[cur] == 0) {
                int temp = nums[cur];
                nums[cur] = nums[zeroRight];
                nums[zeroRight] = temp;
                zeroRight++;
                cur++;
            } else if (nums[cur] == 2) {
                int temp = nums[cur];
                nums[cur] = nums[twoLeft];
                nums[twoLeft] = temp;
                twoLeft--;
            } else {
                cur++;
            }
        }
    }

    private void jishu(int[] nums) {
        int[] count = new int[3];
        for (int num : nums) {
            count[num]++;
        }
        int index = 0;
        for (int i = 0; i < count.length; i++) {
            if (count[i] == 0) {
                continue;
            }
            for (int j = 0; j < count[i]; j++) {
                nums[index] = i;
                index++;
            }
        }
    }

    public static void main(String[] args) {
        Solution solution =  new Solution();
        int[] nums = {2,0,1};
        solution.sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }
}
